﻿<prism:VisualStateAwarePage 
    x:Name="pageRoot"
    x:Class="Sumc.Client.ModernUI.Views.TransportPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sumc.Client.ModernUI.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:behaviors="using:Sumc.Client.ModernUI.Attachements"
    mc:Ignorable="d"
    prism:ViewModelLocator.AutoWireViewModel="True">

    <prism:VisualStateAwarePage.Resources>
        <DataTemplate x:Key="StopItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.GoToScheduleInformation, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/BusStop.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Text="{Binding Name}">
                    </TextBlock>
                    <TextBlock FontSize="14"
                               Margin="0"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap">
                        <Run x:Uid="TransportPageRunNumber" />
                        <Run Text="{Binding Number}" />
                    </TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="GridViewItemZoomOut">
            <Grid Width="400"
                  Height="200"
                  Background="{StaticResource DefaultItemBackground}">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Group.Direction}"
                           VerticalAlignment="Center" 
                           HorizontalAlignment="Center"
                           FontSize="18"
                           TextWrapping="Wrap"
                           Margin="20"/>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="SnapGridViewItemZoomOut">
            <Grid Width="290"
                  Height="200"
                  Background="{StaticResource DefaultItemBackground}">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Group.Direction}"
                           VerticalAlignment="Center" 
                           HorizontalAlignment="Center"
                           FontSize="18"
                           TextWrapping="Wrap"
                           Margin="20"/>
            </Grid>
        </DataTemplate>
        
        <ItemsPanelTemplate x:Key="DirectionsItemsPanel">
            <VirtualizingStackPanel Orientation="Horizontal" 
                                    Margin="110,0,0,0"/>
        </ItemsPanelTemplate>
        <ItemsPanelTemplate x:Key="SnapDirectionsItemsPanel">
            <VirtualizingStackPanel Orientation="Vertical" 
                                    Margin="5,0,0,0"/>
        </ItemsPanelTemplate>

        <CollectionViewSource x:Name="DirectionsViewSource" IsSourceGrouped="true" Source="{Binding Directions}" ItemsPath="Stops" />
    </prism:VisualStateAwarePage.Resources>
    <prism:VisualStateAwarePage.TopAppBar>
        <AppBar>
            <Button Style="{StaticResource HomeAppBarButtonStyle}" 
                    Command="{Binding GoToHome}" />
        </AppBar>
    </prism:VisualStateAwarePage.TopAppBar>

    <Grid Style="{StaticResource LayoutRootStyle}"
          Background="{StaticResource Background}"
          x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="140" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <SemanticZoom x:Name="SemanticZoom"
                      Grid.Row="1"
                      Margin="0,20,0,-3">
            <SemanticZoom.ZoomedOutView>
                <GridView x:Name="DirectionsGriViewZoomOut"
                          ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                          ItemTemplate="{StaticResource GridViewItemZoomOut}">
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Vertical"
                                                    Margin="112,50,120,0"
                                                    Width="410"
                                                    HorizontalAlignment="Left"/>
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>
            </SemanticZoom.ZoomedOutView>

            <SemanticZoom.ZoomedInView>
                <GridView ItemsSource="{Binding Source={StaticResource DirectionsViewSource}}"
                          IsSwipeEnabled="True" 
                          ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                          SelectionMode="None"
                          ScrollViewer.HorizontalScrollBarVisibility="Auto"
                          ItemTemplate="{StaticResource StopItemTemplate}"
                          ItemsPanel="{StaticResource DirectionsItemsPanel}"
                          x:Name="DirectionsGridViewZoomInView">
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="18"
                                               Margin="5,0,0,30"
                                               Foreground="{StaticResource DefaultWhiteBrush}"
                                               Text="{Binding Direction}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,120,0" />
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
            </SemanticZoom.ZoomedInView>
        </SemanticZoom>
        <!--Snap-->
        <SemanticZoom x:Name="SnapSemanticZoom"
                      Grid.Row="1"
                      Margin="5,0,0,-3"
                      Visibility="Collapsed">
            <SemanticZoom.ZoomedOutView>
                <GridView ItemsSource="{Binding ElementName=DirectionsGriViewZoomOut, Path=ItemsSource}"
                          ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                          ItemTemplate="{StaticResource SnapGridViewItemZoomOut}">
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Vertical"
                                                    Margin="0,50,0,0"
                                                    Width="300"
                                                    HorizontalAlignment="Left"/>
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>
            </SemanticZoom.ZoomedOutView>

            <SemanticZoom.ZoomedInView>
                <ListView ItemsSource="{Binding Source={StaticResource DirectionsViewSource}}"
                          IsSwipeEnabled="True" 
                          ScrollViewer.IsHorizontalScrollChainingEnabled="False"
                          SelectionMode="None"
                          ItemTemplate="{StaticResource StopItemTemplate}"
                          ItemsPanel="{StaticResource SnapDirectionsItemsPanel}">
                    <ListView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <TextBlock FontSize="18"
                                               Margin="5,0,0,30"
                                               Foreground="{StaticResource DefaultWhiteBrush}"
                                               Text="{Binding Direction}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,0,0" />
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </ListView.GroupStyle>
                </ListView>
            </SemanticZoom.ZoomedInView>
        </SemanticZoom>
        
        <!-- Back button and page title -->
        <Grid Visibility="{Binding TransportNumber, Converter={StaticResource NullToVisibilityConverter}}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton"
                    Command="{Binding GoBack}"
                    Style="{StaticResource BackButtonStyle}" />
            <Image x:Name="AppLogoImage"
                   Grid.Column="1" Source="ms-appx:///Assets/MainLogo.png"
                   Stretch="None"
                   HorizontalAlignment="Left" NineGrid="0" VerticalAlignment="Top" Margin="0,50,0,0"/>
            <TextBlock x:Name="PageTitle"
                       Grid.Column="3"
                       Style="{StaticResource PageHeaderTextStyle}" HorizontalAlignment="Left" Margin="20,0,30,40">
                <Run Text="{Binding TransportType, Converter={StaticResource TransportTypeToCyrillicConverter}}" />
                <Run Text="{Binding TransportNumber}"/>
                <Run x:Uid="TransportPageRunRout" />
            </TextBlock>
        </Grid>

        <ProgressBar IsIndeterminate="{Binding LoadingData}"
                     Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}}"
                     Style="{StaticResource PageProgressBar}"
                     Grid.Row="1" />

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape" />
                <VisualState x:Name="Filled" />
                <VisualState x:Name="FullScreenPortrait"/>

                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SnapSemanticZoom"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Visible" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SemanticZoom"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <!--Header-->
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitle"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedPageHeaderTextStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="AppLogoImage"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</prism:VisualStateAwarePage>
